﻿using BaiTu.ConfigInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.ConfigFileManager
{
    public class MSMQConfigFileManager : BaseConfigFileManager
    {
        private static MSMQConfigInfo m_configInfo;
        /// <summary>
        /// 文件的最后修改时间
        /// </summary>
        private static DateTime m_fileOldChange;

        static MSMQConfigFileManager()
        {
            if (Common.Helpers.FileHelper.FileExists(ConfigFilePath).Success == true)
            {
                ///如果配置文件存在
                m_fileOldChange = Common.Helpers.FileHelper.GetLastWriteTime(ConfigFilePath);
                m_configInfo = (MSMQConfigInfo)DeserializeInfo(ConfigFilePath, typeof(ConfigInfo.MSMQConfigInfo));
            }
        }
        private static string fileName = null;
        /// <summary>
        /// redis 配置路径
        /// </summary>
        public new static string ConfigFilePath
        {
            get
            {
                if (fileName == null)
                {
                    fileName = Common.Web.Helpers.MapPathHelper.GetPhyicalPath("config/msmq.config");
                }
                return fileName;
            }

        }
        /// <summary>
        /// 配置类信息
        /// </summary>
        public new static BaseConfigInfo ConfigInfo
        {
            get
            {
                return m_configInfo;
            }
            set
            {
                m_configInfo = (MSMQConfigInfo)value;
            }
        }
        /// <summary>
        /// 保存配置类实例
        /// </summary>
        /// <returns></returns>
        public override bool SaveConfig()
        {
            return base.SaveConfig(ConfigFilePath, ConfigInfo);
        }
        public static MSMQConfigInfo LoadConfig()
        {
            if (Common.Helpers.FileHelper.FileExists(ConfigFilePath).Success == true)
            {
                ConfigInfo = LoadConfig(ref m_fileOldChange, ConfigFilePath, ConfigInfo);
                var configInfo = ConfigInfo as MSMQConfigInfo;
                if (configInfo != null)
                {
                    if (configInfo.AutoDeleteNetworkFile == null)
                    {
                        configInfo.AutoDeleteNetworkFile = true;
                    }

                    if (configInfo.MaxQueueBodyLength == null)
                    {
                        configInfo.MaxQueueBodyLength = 3.5 * 1024 * 1024;
                    }
                }
                return configInfo;
            }
            else
                return null;
        }
    }
}
